<?php

namespace app\controller;


use app\middleware\Check;
use app\model\ScopeModel;
use think\facade\Validate;
use think\Request;
use think\response\Json;
use think\validate\ValidateRule;
/**
 * Class Scope
 * @package app\controller
 */
class Scope
{
    protected $middleware = [Check::class];

    /**列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return ScopeModel::list($input, $request->post('token'));
    }

    /**添加
     * @param Request $request
     * @return Json
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['name'] = $request->post('name');
        // 参数校验
        $validate = Validate::rule([
            'name' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return ScopeModel::add($input, $request->post('token'));
    }

    /**获取单个
     * @param Request $request
     * @return Json
     */
    public function getOnly(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return ScopeModel::getOnly($input, $request->post('token'));
    }

    /**编辑
     * @param Request $request
     * @return Json
     */
    public function edit(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return ScopeModel::edit($input, $request->post('token'));
    }

    /**删除管理员
     * @param Request $request
     * @return Json
     */
    public function delete(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return ScopeModel::delete($input, $request->post('token'));
    }
}